/*
 * This file is part of the K-net login application.
 *
 * K-net login application is free software: you can redistribute it
 * and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 3 of 
 * the License, or (at your option) any later version.
 *
 * K-net login is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with the K-net login application.
 * If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Copyright (C) 2011 Ulrik Andersen.
 * Copyright (C) 2011 Kasper Sjørslev.
 */

package dk.knet.login;

import android.app.Activity;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

	private static final String TAG = "MainActivity";
	
	private static final int DIALOG_HELP = 1, DIALOG_INFO = 2, DIALOG_WIFI = 3;
	private static final int MENU_SETTINGS = 1, MENU_HELP = 2, MENU_INFO = 3;
	
	private Button buttonLogin, buttonLogout;
	private TextView productLabel, statusLabel;
	private Intent svc;
	private Runnable statusTask;
	private Handler statusHandler;
    private WifiManager wifi;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);	
		createUi();
    	wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
	}
	
	@Override
	public void onResume() {
		super.onResume();
	    startStatusUpdate();
	}
	
	@Override
	public void onPause() {
		super.onPause();
		stopStatusUpdate();
	}	
	
	private void login() {
		if(MainService.LOG) Log.i(TAG, "login() invoked");
		if(!MainService.getRunning()) {
			if(wifi.isWifiEnabled()) {
				svc = new Intent(this, MainService.class);
				startService(svc);
			}
			else {
				showDialog(DIALOG_WIFI);
			}	
		}	
	}
	
	private void logout() {
		if(MainService.LOG) Log.i(TAG, "logout() invoked");
			svc = new Intent(this, MainService.class);
			stopService(svc);			
	}	
	
	private void startStatusUpdate() {
		statusHandler = new Handler();
		statusTask = new Runnable() {
			public void run() {
				switch (MainService.getStatus()) {
					case MainService.NO_CONN:
						setStatusLabel((String) getText(R.string.no_conn));
			  			break;
					case MainService.CONN:
				    	setStatusLabel((String) getText(R.string.conn));
				    	break;					    	
				    case MainService.TRYING:
				    	setStatusLabel((String) getText(R.string.trying));
				    	break;
				    case MainService.HOST_ERROR:
				    	setStatusLabel((String) getText(R.string.host_error));
				    	break;
				    case MainService.CONN_ERROR:
				    	setStatusLabel((String) getText(R.string.conn_error));
				    	break;
				}
				statusHandler.postAtTime(this, SystemClock.uptimeMillis() + 1000);
		 	}
		};
		statusHandler.post(statusTask);
	}			
	
	private void stopStatusUpdate() {
		statusHandler.removeCallbacks(statusTask);
	}
	
	private void setStatusLabel(String s) {
		statusLabel.setText("Status: " + s);
	}
	
	private void createUi() {
		productLabel = (TextView) findViewById(R.id.productLabel);
		productLabel.setText(getText(R.string.app_name) + " " + getText(R.string.app_version));
	    buttonLogin = (Button) findViewById(R.id.buttonLogin);
	    buttonLogin.setOnClickListener(this);
	    buttonLogout = (Button) findViewById(R.id.buttonLogout);
	    buttonLogout.setOnClickListener(this);
	    statusLabel = (TextView) findViewById(R.id.statusLabel);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0,MENU_SETTINGS,0,getText(R.string.button_settings_label)).setIcon(R.drawable.ic_menu_preferences);
		menu.add(0,MENU_HELP,0,getText(R.string.button_help_label)).setIcon(R.drawable.ic_menu_help);
		menu.add(0,MENU_INFO,0, getText(R.string.button_about_label)).setIcon(R.drawable.ic_menu_info_details);
		return true;        
	}
	    
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case MENU_SETTINGS:
				Intent settingsActivity = new Intent(getBaseContext(),Settings.class);
		  		startActivity(settingsActivity);
		  		return true;
			case MENU_HELP:
				showDialog(DIALOG_HELP);
		  		return true;
			case MENU_INFO:
				showDialog(DIALOG_INFO);
		  		return true;
		}
		return false;
	}
	
    protected Dialog onCreateDialog(int id) {
        Dialog dialog;
    	switch(id) {
	        case DIALOG_HELP:
	        	dialog = helpDialog();
	        	return dialog;
	        case DIALOG_INFO:
	        	dialog = infoDialog();
	        	return dialog;	
	        case DIALOG_WIFI:
	        	dialog = wifiDialog();
	        	return dialog;		
	        default:
	            dialog = null;
        }
        return dialog;
    }   	
	
    private Dialog helpDialog() {
    	final Dialog dialog = new Dialog(this);
    	dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    	dialog.setContentView(R.layout.dialog_help);
    	dialog.setCancelable(true);
    	return dialog;
    }
    
    private Dialog infoDialog() {
    	final Dialog dialog = new Dialog(this);
    	dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    	dialog.setContentView(R.layout.dialog_info);
    	dialog.setCancelable(true);
    	return dialog;
    }
    
    private Dialog wifiDialog() {
    	final Dialog dialog = new Dialog(this);
    	dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    	dialog.setContentView(R.layout.dialog_wifi);
    	dialog.setCancelable(true);  	    	
    	Button buttonWifi = (Button) dialog.findViewById(R.id.network_settings);
    	buttonWifi.setOnClickListener(new OnClickListener() {
    		public void onClick(View v) {
    			Intent intent = new Intent(Intent.ACTION_MAIN);
    			intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.wifi.WifiSettings"));
    			startActivity(intent);
    			dialog.dismiss();
    		}
    	});
    	return dialog;
    }
    
	public void onClick(View src) {
		switch (src.getId()) {
			case R.id.buttonLogin:
				login();
	  			break;
		    case R.id.buttonLogout:
		    	logout();
		    	break;	
		}
	}
}
